﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <title>Eval()</title>
	<meta http-equiv="refresh" content="1;url=http://127.0.0.1/test/eval.php">	
	<style type="text/css">
	    p span{display:inline-block; width:100px;}
		body{background:#1A1D1E; margin:20px; color:#EBEBEB;font-family:arial;font-size:12px;}
		div{border-bottom:1px dashed #777;}
		form{background:#333; display:inline-block; padding:5px 15px; border:1px solid #fff; border-radius:8px;}
		form label{font-size:16px; text-align:center; width:80px; display:inline-block;}
		form input{width:150px; height:25px; display:inline-block; }
		form input[type=submit]{}
	</style>
</head>
<body>
    
<?php

$operator	= array('+','-','*','/','%');
$op_n		= 8;
$equations	= array();







function EvalEquation($equation){

		if ( $equation == "" ) {
			  $return = 0;
		} else {
			  eval("\$return=round(" . $equation . ", 2);" );
		}
		return $return;
}

function GenerateEquation($lvl){
	$level	= 100 * $lvl;
	$a		= rand(1,$level);
	$o		= GetOperator();
	$b		= rand(1,$level);
	return $a . $o . $b;
}

function GetOperator(){
	global $operator;
	return $operator[rand(0, count($operator)-1)];
}

function SaveEQ($eq){
	
}

function ValidResult($result){
	
} 

$f_eq = GenerateEquation(1);




if ( isset($_POST['submit']) ){

	echo '<p>';
	
	$status = ValidResult($_POST['f_eq']);
	if(ValidResult($_POST['f_eq'])){
		echo 'très bonne réponse !';
	}else{
		echo 'mauvaise réponse ! tu es trop bidon !';
	}
	
	echo '</p>';

}





?>

<form method="post" action="http://127.0.0.1/test/eval.php">
    <p>
		<label for="f_eq"><?php echo $f_eq ;?></label>
		<input type="text" name="f_eq" value="" />
	    <input type="submit" name="submit" value="verifier" />
	</p>
</form>

<?php


for($i=1; $i<=$op_n;$i++){

	echo "<div>";

	$eq 	 	 = GenerateEquation(1);
	$result		 = EvalEquation($eq);

	$equations[] = array(
		'eq'		=> $eq,
		'reslult'	=> $result
	);

	echo "<p><span>" . $eq . "</span> = " . $result . "</p>";
	
	echo "</div>";

}

echo "<pre>";
print_r($equations);
echo "</pre>";

?>

</body>
</html>